package Test.Product;


import java.util.*;



public class ProductTest {
    public static void main(String[] args) {
        List<Product> list = new ArrayList<>();
        Product p1 = new Product("宝洁","洗手粉",18.5);
        Product p2 = new Product("联合利华","肥皂",4.5);
        Product p3 = new Product("宝洁","牙膏",32.5);
        Product p4 = new Product("宝洁","毛巾",14.5);
        Product p5 = new Product("洁利","洗面奶",26.0);
        Product p6 = new Product("好迪","洗发水",27.5);
        Product p7 = new Product("多芬","沐浴露",38.5);
        Product p8 = new Product("宝洁","洗洁精",3.4);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        list.add(p6);
        list.add(p7);
        list.add(p8);

        //  method02(list);
        test04(list);
    }

    private static void test04(List<Product> list) {
    Map<String,List<Product>> map=new HashMap<>();
        for (Product p:list
             ) {
            String brand=p.getBrand();
            System.out.println(brand);
            if(map.containsKey(brand)){
                List<Product> list1=new ArrayList<>();
                list1.add(p);
                map.put(brand,list1);

            }
            else {
                List<Product> productList=new ArrayList<>();
                productList.add(p);
                map.put(brand,productList);
            }
        }
        map.forEach((k,v)-> System.out.println(k+":"+v));
        Set<String> keySet=map.keySet();
        Map<String,Double> map1=new HashMap<>();
        Iterator<String> inter=keySet.iterator();
        while (inter.hasNext()){
            String br=inter.next();
            List<Product> list2=map.get(br);
            double totle=0.0d;
            for (Product p1:list2) {
                totle+=p1.getCost();
                map1.put(br,totle);
            }
            map1.put(br,totle);
            System.out.println(map1);
        }
        Set<Map.Entry<String,Double>> keysets=map1.entrySet();

        List<Map.Entry<String,Double>> list1=new ArrayList<>();
        Iterator<Map.Entry<String,Double>> iterator1=keysets.iterator();
        while (iterator1.hasNext()){
            list1.add(iterator1.next());
        }
        list1.sort((o1,o2)->(int)(o2.getValue()-o1.getValue()));
        list1.forEach(System.out::println);
    }
}

